/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.editor.doc;

import javax.swing.text.*;
/**
 *
 * @author Administrator
 */
    public class IntegerDocument extends PlainDocument {
    
    private int limit;
    String currentValue = "";

    public IntegerDocument(int limit) {
        super();
        this.limit = limit;
    }

    public String getValue() {
        return currentValue;
    }

    @Override
    public void insertString(int offset, String string, AttributeSet attributes) throws BadLocationException {
        if (string == null) {
        } else {
            String newValue;
            int length = getLength();
            if (length == 0) {
                newValue = string;
            } else {
                String currentContent = getText(0, length);
                StringBuilder currentBuffer = new StringBuilder(currentContent);
                currentBuffer.insert(offset, string);
                newValue = currentBuffer.toString();
            }
            currentValue = checkInput(newValue, offset);

            if ((getLength() + string.length()) <= limit) {
                super.insertString(offset, string, attributes);
            }
            //super.insertString(offset, string, attributes);
        }
    }

    @Override
    public void remove(int offset, int length) throws BadLocationException {
        int currentLength = getLength();
        String currentContent = getText(0, currentLength);
        String before = currentContent.substring(0, offset);
        String after = currentContent.substring(length+offset,
          currentLength);
        String newValue = before + after;
        currentValue = checkInput(newValue, offset);
        super.remove(offset, length);
    }

    public String checkInput(String proposedValue, int offset) throws BadLocationException {
        //System.out.println("proposedValue :" + proposedValue);
        //System.out.println("offset :" + offset);
        
        if (proposedValue.length() > 0) {
            try {
                if(proposedValue.equals("-")){
                    if(!getText(0, offset).contains("-")){
                        return proposedValue;
                    } else {
                        if(proposedValue.equals("-")){
                            return proposedValue;
                        } else {
                            return String.valueOf(Integer.parseInt(proposedValue));
                        }
                        
                    }   
                } else {
                    return String.valueOf(Integer.parseInt(proposedValue));
                }
            } catch (NumberFormatException e) {
                 throw new BadLocationException(proposedValue, offset);
            }
        } else {
            return "";
        }
    }
}

